<?php
$user = new User();
$shop = new Shop();
$pro = new Product();
$shop->checkUser();//检查用户合法性，防止攻击

$cart = $shop->getCart();
if(!$cart)echo "<script type='text/javascript'>location.href='/home';</script>";
$address = $shop->getUserAddress();
$pp = 0;

if(isset($_POST['submit'])){
	$sql = "select orders_id from speed_orders order by orders_id desc limit 1";
	$id = $shop->query($sql,'one');
	$orders_id = $id['orders_id']+1;//运单号加1
	$total = $shop->getCartTotal();
	$oo = number_format($orders_id);
	$oo = str_replace(",","",$oo);
	if($_POST['address_chose'] == 'new'){
		$province = $shop->getPlace($_POST['province'],'self');
		$city = $shop->getPlace($_POST['city'],'self');
		$area = $shop->getPlace($_POST['area'],'self');
		$data = array("province" => $province['region_name'],
						   "city" => $city['region_name'],
						   "area" => $area['region_name'],
						   "addressee" => $_POST['true_name'],
						   "address" => $_POST['address'],
						   "phone_number" => $_POST['tel'],
						   "zip_code" => $_POST['zip_code'],
						   "create_user" => $user->getUserId("user_email"),
						   "create_time" => date("Y-m-d H:i:s",time()));
		$ad = $shop->addAddress($data,'id');
	}else{
		$ad = $_POST['address_chose'];
	}
	
	//echo $oo;exit();
	$data = array("orders_id" => $oo,
						  "create_user" => $user->getUserId("user_email"),
						  "total" => $total,
						  "freight" => constant(strtoupper($_POST['ex_chose'])),
						  "express" => $_POST['ex_chose'],
						  "address" => $ad,
						  "create_time" => date("Y-m-d H:i:s",time()));
						  
	$id = $shop->addOrder($data);
	$href = '/orders/'.$id;
	echo "<script type='text/javascript'>location.href='$href';</script>";
	//else echo "<script type='text/javascript'>alert('非法操作');</script>";
}

?>